﻿using System;

namespace OrderManager.Domain.Entities
{
    /// <summary>
    /// Represents order entity of repository 
    /// </summary>
    public class Order : EntityBase
    {
        /// <summary>
        /// Represents id of customer for current order
        /// </summary>
        private Int32 _customerId;
        /// <summary>
        /// Represents an instance of <see cref="OrderManager.Domain.Entities.Customer"/> class for current order
        /// </summary>
        private Customer _customer;

        /// <summary>
        /// Initialize a new instance of <see cref="OrderManager.Domain.Entities.Order"/> class
        /// </summary>
        public Order()
        {
        }

        /// <summary>
        /// Initialize a new instance of <see cref="OrderManager.Domain.Entities.Order"/> class with specified id, number, date, amount, description and belonged to customer with specified id
        /// </summary>
        /// <param name="id">Order's id</param>
        /// <param name="number">Order's number</param>
        /// <param name="orderDate">Order's date</param>
        /// <param name="amount">Order's amount</param>
        /// <param name="descripton">Order's description</param>
        /// <param name="customerId">Id of customer current order belonges to</param>
        public Order(Int32 id,
                     Int32 number,
                     DateTime orderDate,
                     Int32 amount,
                     String descripton,
                     Int32 customerId)
            : base(id)
        {
            Number = number;
            OrderDate = orderDate;
            Amount = amount;
            Desctiption = descripton;
            _customerId = customerId;
        }

        /// <summary>
        /// Represents number of current order
        /// </summary>
        public int Number { get; set; }

        /// <summary>
        /// Represents date of current order
        /// </summary>
        public DateTime OrderDate { get; set; }

        /// <summary>
        /// Represents amount of current order
        /// </summary>
        public int Amount { get; set; }

        /// <summary>
        /// Represents description of current order
        /// </summary>
        public string Desctiption { get; set; }

        /// <summary>
        /// Gets a customer current order belongs to (lazy)
        /// </summary>
        public Customer Customer
        {
            get
            {
                if (_customer == null)
                {
                    _customer = RepositoryFactory.GetCustomerRepository().GetById(_customerId);
                }
                return _customer;
            }
        }

        /// <summary>
        /// Deletes current order from repository
        /// </summary>
        public void Delete()
        {
            RepositoryFactory.GetOrderRepository().Delete(Id);
        }

        /// <summary>
        /// Update repository entity with current order properties values
        /// </summary>
        public void Save()
        {
            RepositoryFactory.GetOrderRepository().Update(this);
        }
    }
}